﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Zyg.Security;

namespace Zyg.SSO.Certification
{
    /// <summary>
    /// Summary description for TicketAuthentication
    /// </summary>
    [WebService(Namespace = "Zyg.SSO.Certification")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class TicketAuthentication : System.Web.Services.WebService
    {

        [WebMethod]
        public bool LegitimateTicket(string apId, string requestIP, string ticketstring)
        {
            SecurityHelper shelper = new SecurityHelper();
            ticketstring = shelper.Decrypt(ticketstring, EncryptionAlgorithm.Des);

            bool bLegitimate = false;
            Zyg.SSO.Core.Ticket ticket = new Zyg.SSO.Core.Ticket();

            try
            {
                ticket = Zyg.SSO.Core.Ticket.DeSerialize(ticketstring);

                #region 验证 AP 标识

                if (apId != ticket.AppId)
                {
                    bLegitimate = false;

                    return bLegitimate;
                }

                #endregion

                #region 验证 IP

                if (requestIP != ticket.IP)
                {
                    bLegitimate = false;

                    return bLegitimate;
                }

                #endregion

                #region 验证时间

                DateTime dtTicket = ticket.CreatedTime;
                DateTime dtNow = DateTime.Now;
                TimeSpan tsTicket = new TimeSpan(dtTicket.Ticks);
                TimeSpan tsNow = new TimeSpan(dtNow.Ticks);

                TimeSpan tsSubtract = tsNow.Subtract(tsTicket).Duration();

                if (tsSubtract.Days == 0)
                {
                    if (tsSubtract.Hours == 0)
                    {
                        if (tsSubtract.Minutes < 1)
                        {
                            bLegitimate = true;
                        }
                    }
                }

                #endregion
            }
            catch
            {
                bLegitimate = false;
            }

            return bLegitimate;
        }


        [WebMethod]
        public string GetTicket(string UserId, string UserIP, string APID)
        {
            Zyg.SSO.Core.Ticket ticket = new Zyg.SSO.Core.Ticket();

            ticket.AppId = APID;
            ticket.CreatedTime = DateTime.Now;
            ticket.IP = UserIP;
            ticket.UserId = UserId;

            // 序列化
            string strTicket = Zyg.SSO.Core.Ticket.Serialize(ticket);

            SecurityHelper shelper = new SecurityHelper();
            strTicket = shelper.Encrypt(strTicket, EncryptionAlgorithm.Des); // 加密

            return strTicket;
        }
    }
}
